package xml.saxParserExample;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;


public class ShowBookDialog extends Dialog{

	private Shell shell = null;
	private TableViewer tableViewer = null;
	protected ShowBookDialog(Shell shell) {
		super(shell);
		this.shell = shell;
	}
	protected Control createDialogArea(Composite parent) {
		  Composite composite = (Composite) super.createDialogArea(parent);
		  Composite tableComposite = new Composite(composite, SWT.NONE);
		  tableComposite.setLayout(new GridLayout());
		  tableViewer = new TableViewer(tableComposite, SWT.MULTI|SWT.CHECK|SWT.BORDER|SWT.H_SCROLL|SWT.V_SCROLL);
		  
		  Table table = tableViewer.getTable();
		  table.setLinesVisible(true);
		  table.setHeaderVisible(true);
		
		  final TableColumn idColumn = new TableColumn(table, SWT.NONE);
		  idColumn.setWidth(150);
		  idColumn.setText("ISBN");
		  
		  final TableColumn nameColumn = new TableColumn(table, SWT.NONE);
		  nameColumn.setWidth(150);
		  nameColumn.setText("NAME");
		  
		  final TableColumn authorColumn = new TableColumn(table, SWT.NONE);
		  authorColumn.setWidth(100);
		  authorColumn.setText("AUTHOR");
		  
		  
		  final TableColumn iconColumn = new TableColumn(table, SWT.NONE);
		  iconColumn.setWidth(50);
		  iconColumn.setText("ICON");
		  
		  tableViewer.setContentProvider(new BooksContentProvider());
		  tableViewer.setLabelProvider(new BooksLabelProvider());
		  tableViewer.setInput(getBooks());

		  composite.pack();
		  return composite;
	}
	
	private List<Book> getBooks(){
		List<Book> books = new ArrayList<Book>();
		BooksSAXHandler.refresh();
		for(String id : BooksSAXHandler.getBookIDs())
		{
			books.add(BooksSAXHandler.getBook(id));
		}
		return books;
	}
	
	class BooksContentProvider implements IStructuredContentProvider {

		public Object[] getElements(Object inputElement) {
			if(inputElement instanceof List){
				return ((List)inputElement).toArray();
			}else{
				return new Object[0];
			}
		}

		public void dispose() {
		}

		public void inputChanged(Viewer arg0, Object arg1, Object arg2) {			
		}
		
	}
	
	class BooksLabelProvider implements ITableLabelProvider{

		public Image getColumnImage(Object element, int columnIndex) {
			if (element instanceof Book){
				Book book = (Book)element;
				if(columnIndex == TableConstants.COLUMN_ICON)
				{
					return book.getImage();
				}
			}
			return null;
		}

		public String getColumnText(Object element, int columnIndex) {
			if (element instanceof Book){
				Book book = (Book)element;
				if(columnIndex == TableConstants.COLUMN_ID)
				{
					return book.getID();
				}
				else if(columnIndex == TableConstants.COLUMN_NAME)
				{
					return book.getName();
				}
				else if(columnIndex == TableConstants.COLUMN_AUTHOR)
				{
					return book.getAuthor();
				}
			}
			return null;
		}

		public void addListener(ILabelProviderListener arg0) {
		}

		public void dispose() {		
		}

		public boolean isLabelProperty(Object arg0, String arg1) {
			return false;
		}

		public void removeListener(ILabelProviderListener arg0) {
		}
		
	}
	
	
}
